package com.dev.dream.exochatlockscreen.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dev.dream.exochatlockscreen.LockScreenAppActivity;
import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.exochatlockscreen.model.NotifiItem;
import com.dev.dream.libs.slidemenu.circleimageview.AWCircleImageView;

public class NotifiExoAdapter extends BaseAdapter {

	LockScreenAppActivity activity;
	private Context context;
	private ArrayList<NotifiItem> notifiItems;

	public NotifiExoAdapter(Context context, ArrayList<NotifiItem> appToolbar,
			LockScreenAppActivity activity) {
		this.context = context;
		this.notifiItems = appToolbar;
		this.activity = activity;

	}

	@Override
	public int getCount() {
		return notifiItems.size();
	}

	@Override
	public Object getItem(int position) {
		return notifiItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {

		TextView txtDate;
		TextView txtName;
		TextView txtBody;
		// LinearLayout llSms;
		AWCircleImageView img_avatar;
		ImageView img_close;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_notifi_exo, null);

			holder = new ViewHolder();

			holder.txtDate = (TextView) convertView
					.findViewById(R.id.tv_date_notifi_exo);
			holder.txtName = (TextView) convertView
					.findViewById(R.id.tv_name_notifi_exo);
			holder.txtBody = (TextView) convertView
					.findViewById(R.id.tv_content_notifi_exo);
			// holder.llSms = (LinearLayout) convertView
			// .findViewById(R.id.ll_notifi_exo);
			holder.img_avatar = (AWCircleImageView) convertView
					.findViewById(R.id.img_avatar_notifi_exo);
			holder.img_close = (ImageView) convertView
					.findViewById(R.id.img_close_notifi_exo);

			convertView.setTag(holder);

			holder.txtBody.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					NotifiItem info = (NotifiItem) v.getTag();
					if (info.getType() == NotifiItem.SMS) {
						Intent defineIntent = new Intent(Intent.ACTION_VIEW);
						defineIntent.setData(Uri
								.parse("content://mms-sms/conversations/"
										+ info.getThread_id()));
						activity.startActivity(defineIntent);
					} else {
						Intent intentCall = new Intent(Intent.ACTION_VIEW, Uri
								.parse("content://call_log/calls")); // Intent(Intent.ACTION_VIEW,ContactsContract.Contacts.CONTENT_URI);
						activity.startActivity(intentCall);
					}
					// Toast.makeText(context, info.getId() + "", 1000).show();
				}
			});

			holder.img_close.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try {

						NotifiItem info = (NotifiItem) v.getTag();

						if (info.getType() == NotifiItem.SMS) {
							ContentValues values = new ContentValues();
							values.put("read", true);
							context.getContentResolver().update(
									Uri.parse("content://sms/inbox"), values,
									"_id=" + info.getId(), null);

							notifiItems.remove(info.getPosition());
							activity.getNotifi();
						} else {
							Uri CALLLOG_URI = CallLog.Calls.CONTENT_URI;
							ContentValues values = new ContentValues();
							values.put(Calls.NEW, 0);
							try {
								context.getContentResolver().update(
										CALLLOG_URI,
										values,
										"_id=?",
										new String[] { String.valueOf(info
												.getId()) });
								activity.getNotifi();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}

					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			});

		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		// convertView.setClickable(true);

		// imgIcon.setImageResource(appAdsItem.get(position).getIcon());
		NotifiItem info = notifiItems.get(position);
		holder.txtDate.setText(info.getDate());
		holder.txtName.setText(info.getName());
		holder.txtBody.setText(info.getBody());
		holder.txtBody.setTag(info);
		holder.img_close.setTag(info);

		if (info.getType() == NotifiItem.MISS_CALL) {
			holder.img_avatar.setImageResource(R.drawable.phone_head);
		} else {
			holder.img_avatar.setImageResource(R.drawable.sms_head);
		}

		// if (info.getType() == NotifiSmsItem.SMS) {
		// holder.img_avatar.setVisibility(View.GONE);
		// } else {
		// holder.img_close.setVisibility(View.GONE);
		// }

		return convertView;
	}

}
